<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">
        <i class="el-icon-s-home"></i>
        首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>
        <i class="el-icon-present" ></i> 
        产品类别
      </el-breadcrumb-item>
      <!-- 当前 -->
      <el-breadcrumb-item>{{jiahuogai}}</el-breadcrumb-item>
    </el-breadcrumb>
    <header>
      <div><i class="el-icon-arrow-left" @click="gotop" ></i></div>
      <h3>{{jiahuogai}}</h3>
    </header>
    <div id="tijiao">
      <!-- 添加 -->
     <el-form ref="form" :model="form" label-width="120px" :rules="rules"  v-if="a==0" >
        <el-form-item label="品牌名称:"     prop="name">
          <el-input v-model="form.name"  placeholder="输入商品名称"></el-input>
        </el-form-item>
        <el-form-item label="排序:"   >
          <el-input v-model="form.px" placeholder="输入标题"></el-input>
        </el-form-item>
        <el-form-item label="品牌logo:"  s>
          <el-upload
            action="http://127.0.0.1:7001/upload"
            list-type="picture-card"
            :on-success="ok">
              <i slot="default" class="el-icon-plus"></i>
              <div slot="file" slot-scope="{file}">
                <img
                  class="el-upload-list__item-thumbnail"
                  :src="file.url" alt=""
                >
                <span class="el-upload-list__item-actions">
                  <span
                    class="el-upload-list__item-preview"
                    @click="handlePictureCardPreview(file)"
                  >
                    <i class="el-icon-zoom-in"></i>
                  </span>
                  <span
                    v-if="!disabled"
                    class="el-upload-list__item-delete"
                    @click="handleRemove(file)"
                  >
                    <i class="el-icon-delete"></i>
                  </span>
                </span>
              </div>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible"  append-to-body>
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="品牌轮播:" >
          <el-upload
            action="http://127.0.0.1:7001/upload"
            list-type="picture-card"
            :on-success="oks">
              <i slot="default" class="el-icon-plus"></i>
              <div slot="file" slot-scope="{file}">
                <img
                  class="el-upload-list__item-thumbnail"
                  :src="file.url" alt=""
                >
                <span class="el-upload-list__item-actions">
                  <span
                    class="el-upload-list__item-preview"
                    @click="handlePictureCardPreview(file)"
                  >
                    <i class="el-icon-zoom-in"></i>
                  </span>
                  <span
                    v-if="!disabled"
                    class="el-upload-list__item-delete"
                    @click="handleRemove(file)"
                  >
                    <i class="el-icon-delete"></i>
                  </span>
                </span>
              </div>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible"  append-to-body>
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="首页展示：">
          <el-radio v-model="form.zs" label="启用">启用</el-radio>
          <el-radio v-model="form.zs" label="禁用">禁用</el-radio>
        </el-form-item>
        <el-form-item label="展示位置:"  >
          <el-input v-model="form.location" placeholder="输入数量"></el-input>
        </el-form-item>
        <el-form-item style="width:100%;display:flex;justify-content: center;">
          <el-button type="primary" @click="onSubmit">保存并上架</el-button>
          <el-button type="primary" @click="onSubmit">保存到库存</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
      <!-- 修改 -->
      <el-form ref="form" :model="form" label-width="120px" :rules="rules"  v-else >
        <el-form-item label="品牌名称:"     prop="name">
          <el-input v-model="form.name"  :placeholder="list.name"></el-input>
        </el-form-item>
        <el-form-item label="排序:"   >
          <el-input v-model="form.px" :placeholder="list.px"></el-input>
        </el-form-item>
        <el-form-item label="品牌logo:"  s>
          <el-upload
            action="http://127.0.0.1:7001/upload"
            list-type="picture-card"
            :on-success="ok">
              <i slot="default" class="el-icon-plus"></i>
              <div slot="file" slot-scope="{file}">
                <img
                  class="el-upload-list__item-thumbnail"
                  :src="file.url" alt=""
                >
                <span class="el-upload-list__item-actions">
                  <span
                    class="el-upload-list__item-preview"
                    @click="handlePictureCardPreview(file)"
                  >
                    <i class="el-icon-zoom-in"></i>
                  </span>
                  <span
                    v-if="!disabled"
                    class="el-upload-list__item-delete"
                    @click="handleRemove(file)"
                  >
                    <i class="el-icon-delete"></i>
                  </span>
                </span>
              </div>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible"  append-to-body>
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="品牌轮播:" >
          <el-upload
            action="http://127.0.0.1:7001/upload"
            list-type="picture-card"
            :on-success="oks">
              <i slot="default" class="el-icon-plus"></i>
              <div slot="file" slot-scope="{file}">
                <img
                  class="el-upload-list__item-thumbnail"
                  :src="file.url" alt=""
                >
                <span class="el-upload-list__item-actions">
                  <span
                    class="el-upload-list__item-preview"
                    @click="handlePictureCardPreview(file)"
                  >
                    <i class="el-icon-zoom-in"></i>
                  </span>
                  <span
                    v-if="!disabled"
                    class="el-upload-list__item-delete"
                    @click="handleRemove(file)"
                  >
                    <i class="el-icon-delete"></i>
                  </span>
                </span>
              </div>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible"  append-to-body>
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="首页展示：">
          <el-radio v-model="form.zs" label="启用">启用</el-radio>
          <el-radio v-model="form.zs" label="禁用">禁用</el-radio>
        </el-form-item>
        <el-form-item label="展示位置:"  >
          <el-input v-model="form.location" :placeholder="list.location"></el-input>
        </el-form-item>
        <el-form-item style="width:100%;display:flex;justify-content: center;">
          <el-button type="primary" @click="onSubmit">保存并上架</el-button>
          <el-button type="primary" @click="onSubmit">保存到库存</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
      
    </div>
    
  </div>
</template>

<script>
import { getzwyLogoList, postzwyLogoListtAdd, putzwyLogoListUpdate } from '@/utils/api';
export default {
  data(){
    return{
      input:'',
      a:0,
      jiahuogai:'添加品牌',
      list:{},
      form: {
        name:'',
        px:'',
        img:'',
        images:'',
        zs:'',
        location:''
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
      },
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false
    }
  },
  methods:{
    onSubmit() {
      if(this.a==0){
        console.log(this.form);
        postzwyLogoListtAdd(this.form).then(res=>console.log(res))
      }else{
        console.log('submit!');
        const obj = _.pickBy({...this.form})
        putzwyLogoListUpdate({...obj ,id:this.a})
        this.$alert('修改成功', '成功', {
          confirmButtonText: '确定',
          callback: action => {this.$router.push('/shop/brand')}
        });
      }
      
    },
    handleRemove(file) {
        console.log(file);
    },
    ok(file,list) {
      const obj = file.data.replaceAll('\\\\', '\\/')
      const http = "http://127.0.0.1:7001" + obj
      const imghttp = http.replaceAll("\\","\/")
      this.form.img = imghttp
    },
    oks(file,list) {
      const obj = file.data.replaceAll('\\\\', '\\/')
      const http = "http://127.0.0.1:7001" + obj
      const imghttp = http.replaceAll("\\","\/")
      this.form.images = imghttp
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    gotop(){
      this.$router.go(-1)
    },
  },
  created(){
    if(this.$route.params.id>0){
      this.a = this.$route.params.id
      this.jiahuogai = '修改商品'
      getzwyLogoList().then(res=>{
        let bb = res.data.data.filter(item=>item.id == this.$route.params.id)[0]
        console.log(bb);
        this.list = bb
      })
    }
  },
  
}
</script>

<style scoped>
header{
  width: 100%;
  height: 40px;
  line-height: 40px;
  background-color:rgba(185, 182, 182, 0.5);
  display: flex;
  margin-top:10px ;
  margin-bottom:10px ;
}
header div{
  width: 3%;
  height: 40px;
  line-height: 40px;
  font-size: 30px;
}
#tijiao{
  width: 65%;
  height: 100%;
  margin: 0 auto;
}
</style>